#include "qdatetimeedithost.h"

#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QDateTimeEdit>

using namespace SHAREDLIB;

QDateTimeEditHost::QDateTimeEditHost(QObject *object):
    QAbstractSpinBoxHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("DateTime");
    if(pro!=NULL)
    {
        pro->setAttribute("name","maximumDateTime");
        pro->setAttribute("property_name",tr("MaximumDateTime"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("DateTime");
    if(pro!=NULL)
    {
        pro->setAttribute("name","minimumDateTime");
        pro->setAttribute("property_name",tr("MinimumDateTime"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("DateTime");
    if(pro!=NULL)
    {
        pro->setAttribute("name","dateTime");
        pro->setAttribute("property_name",tr("DateTime"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","currentSection");
        pro->setAttribute("property_name",tr("CurrentSection"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("YearSection");
        item.m_value=QDateTimeEdit::YearSection;
        items.append(item);

        item.m_text=tr("MonthSection");
        item.m_value=QDateTimeEdit::MonthSection;
        items.append(item);

        item.m_text=tr("SecondSection");
        item.m_value=QDateTimeEdit::DaySection;
        items.append(item);

        item.m_text=tr("HourSection");
        item.m_value=QDateTimeEdit::HourSection;
        items.append(item);

        item.m_text=tr("MinuteSection");
        item.m_value=QDateTimeEdit::MinuteSection;
        items.append(item);

        item.m_text=tr("SecondSection");
        item.m_value=QDateTimeEdit::SecondSection;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        pro->setValue(QDateTimeEdit::YearSection);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","dateTimeChanged");
        pro->setAttribute("property_name",tr("DateTimeChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("dateTimeChanged","");
        addProperty(pro);
    }


    QDateTimeEdit *e=(QDateTimeEdit*)m_object;
    connect(e,SIGNAL(dateTimeChanged(QDateTime)),this,SLOT(dateTimeChanged(QDateTime)));
}

void QDateTimeEditHost::setDateTime(int year, int month, int day, int hour, int minute, int second)
{
    QDateTime dt(QDate(year,month,day),QTime(hour,minute,second));
    setPropertyValue("dateTime",dt);
}

int QDateTimeEditHost::year()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.date().year();
}

int QDateTimeEditHost::month()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.date().month();
}

int QDateTimeEditHost::day()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.date().day();
}

int QDateTimeEditHost::hour()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.time().hour();
}

int QDateTimeEditHost::minute()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.time().minute();
}

int QDateTimeEditHost::second()
{
    QDateTime dt=getPropertyValue("dateTime").toDateTime();
    return dt.time().second();
}

void QDateTimeEditHost::setCurrentSection(int currentSection)
{
    setPropertyValue("currentSection",currentSection);
}

int QDateTimeEditHost::currentSection()
{
    return getPropertyValue("currentSection").toInt();
}

void QDateTimeEditHost::dateTimeChanged(const QDateTime &datetime)
{
    QString code=getPropertyValue("dateTimeChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("year",QString::number(datetime.date().year()));
        param.insert("month",QString::number(datetime.date().month()));
        param.insert("day",QString::number(datetime.date().day()));
        param.insert("hour",QString::number(datetime.time().hour()));
        param.insert("minute",QString::number(datetime.time().minute()));
        param.insert("second",QString::number(datetime.time().second()));
        emit_exec(code,param);
    }
}
